package com.ssm.pre.service.impl;

import java.util.List;
import java.util.Map;

import org.apache.commons.codec.binary.Base64;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ssm.pre.util.PageCount;
import com.ssm.pre.util.ToolUtil;
import com.ssm.pre.dao.UserDao;
import com.ssm.pre.service.UserService;
import com.ssm.util.object.InputObject;
import com.ssm.util.object.OutputObject;
import com.ssm.wechat.dao.WechatUserDao;

@Service
public class UserServiceImpl implements UserService{
	
	@Autowired
	private UserDao userDao;

	@Autowired
	private WechatUserDao wechatUserDao;
	/**
	 * 登录验证
	 */
	@Override
	public void queryAdminByLogin(InputObject inputObject, OutputObject outputObject) throws Exception{
		Map<String, Object> map = inputObject.getParams();
		//查询管理员是否存在
		Map<String, Object> bean = userDao.queryAdminByLogin(map);
		if(bean==null){
			outputObject.setreturnMessage("该用户账号不存在.");
		}else{
			if(!bean.get("password").toString().equals(ToolUtil.MD5(map.get("password").toString()))){
				outputObject.setreturnMessage("密码错误.");
			}else{
				//查询所有的菜单
				List<Map<String, Object>> beans =  userDao.queryAllMenu(bean);
				beans = ToolUtil.createDataToMenu(beans);
				outputObject.setLogParams(bean);//将登陆人信息存入session中
				outputObject.setLogMenuParams(beans);//将菜单存入session中
			}
		}
	}
	
	/**
	 * 查询用户管理员列表
	 */
	@Override
	public void queryAdminByList(InputObject inputObject, OutputObject outputObject) throws Exception {
		
		Map<String,Object> map = inputObject.getParams();
		List<Map<String, Object>> beans = userDao.queryAdminByList(map);
		int page = Integer.parseInt(map.get("offset").toString())/Integer.parseInt(map.get("limit").toString());
		page++;
		List<Map<String, Object>> params = PageCount.page(beans, page, Integer.parseInt(map.get("limit").toString()));
		outputObject.setBeans(params);
		outputObject.settotal(beans.size()); 
		 

	}

	/**
	 * 获取登录信息
	 */
	@Override
	public void queryUserByLogin(InputObject inputObject, OutputObject outputObject) throws Exception {
		Map<String, Object> bean = inputObject.getLogParams();
		List<Map<String, Object>> beans = inputObject.getLogMenuParams();
		if(bean == null){
			outputObject.setreturnMessage("登录信息为空，请3秒后重新登录.");
		}else{
			outputObject.setBean(bean);
			outputObject.setBeans(beans);
		}
	}

	/**
	 * 增加维修人员信息
	 */
	@Override
	public void addServiceMan(InputObject inputObject, OutputObject outputObject) throws Exception {
		Map<String, Object> map = inputObject.getParams();
		map.put("createId", inputObject.getLogParams().get("id"));
		map.put("createTime", ToolUtil.getTimeAndToString());
		userDao.addServiceMan(map);
	}

	/**
	 * 根据ID删除维修人员
	 */
	@Override
	public void deleteServiceManById(InputObject inputObject, OutputObject outputObject) throws Exception {
		Map<String, Object> map = inputObject.getParams();
		userDao.deleteServiceManById(map);
		
	}

	/**
	 * 根据ID修改维修人员信息
	 */
	@Override
	public void updateServiceManById(InputObject inputObject, OutputObject outputObject) throws Exception {
		Map<String, Object> map = inputObject.getParams();
		Map<String, Object> user = inputObject.getLogParams();
		map.put("createId", user.get("id"));
		map.put("createTime", ToolUtil.getTimeAndToString());
		userDao.updateServiceManById(map);
		
	}

	/**
	 * 查询维修人员信息
	 */
	@Override
	public void queryServiceManByList(InputObject inputObject, OutputObject outputObject) throws Exception {
		Map<String, Object> map = inputObject.getParams();
		List<Map<String, Object>> beans = userDao.queryServiceManByList(map);
		int page = Integer.parseInt(map.get("offset").toString())/Integer.parseInt(map.get("limit").toString());
		page++;
		List<Map<String, Object>> params = PageCount.page(beans, page, Integer.parseInt(map.get("limit").toString()));
		outputObject.setBeans(params);
		outputObject.settotal(beans.size());
	}

	/**
	 * 根据id查询维修人员
	 * */
	@Override
	public void queryServiceManOneById(InputObject inputObject, OutputObject outputObject) throws Exception {
		Map<String, Object> map = inputObject.getParams();
		Map<String, Object> bean = userDao.queryServiceMantOneById(map);
		outputObject.setBean(bean);
		
	}

	/**
	 * 查找微信用户信息
	 */
	@Override
	public void queryWechatUserByList(InputObject inputObject, OutputObject outputObject) throws Exception {
		Map<String, Object> map = inputObject.getParams();
		if(map.get("searchParams")!=null){
			String nickname = Base64.encodeBase64String(map.get("searchParams").toString().getBytes("utf-8"));;
			map.put("nickname", nickname);
		}
		//分页查找所有的微信用户信息
		List<Map<String, Object>> beans = wechatUserDao.queryWechatUserByList(map);
		int page = Integer.parseInt(map.get("offset").toString())/Integer.parseInt(map.get("limit").toString());
		page++;
		List<Map<String, Object>> params = PageCount.page(beans, page, Integer.parseInt(map.get("limit").toString()));
		outputObject.setBeans(params);
		outputObject.settotal(beans.size());
		
	}

	/**
	 * 根据ID删除一条记录
	 */
	@Override
	public void deleteWechatUserById(InputObject inputObject, OutputObject outputObject) throws Exception {
		Map<String, Object> map = inputObject.getParams();
		wechatUserDao.deleteWechatUserById(map);
	}
}
